PaymentsGridPanel = function(limit){
	var store = new Ext.data.JsonStore(
		{
			url:__ctxPath+'/back/fees/findpayments.html',
			totalProperty:'totalCount',
			root:'data',
			baseParams:{start:0,limit:limit},
			autoLoad:true,
			fields:['fid','area','address','username','userid','minuspay',
			'buildarea','feearea','shouldpay','realpay','discount',
			'receiptno','paydate','telephone','oweyear','payyear','notes']
		}
	);
	//建立columnmodel 信息
	var sc = new Ext.grid.CheckboxSelectionModel();
	var cm = [
		new Ext.grid.RowNumberer(),
		sc,
		{header:'主键ID',dataIndex:'fid',sortable: true,width:50},
		{header:'小区名称',dataIndex:'area',sortable: true,width:200},
		{header:'住址',dataIndex:'address',sortable: true,width:200},
		{header:'用户名称',dataIndex:'username',sortable: true,width:200},
		{header:'建筑面积',dataIndex:'buildarea',sortable: true,width:200},
		{header:'采暖面积',dataIndex:'feearea',sortable: true,width:200},
		{header:'应收金额',dataIndex:'shouldpay',sortable: true,width:200},
		{header:'实收金额',dataIndex:'realpay',sortable: true,width:200},
		{header:'优惠金额',dataIndex:'discount',sortable: true,width:200},
		{header:'票号',dataIndex:'receiptno',sortable: true,width:200},
		{header:'日期',dataIndex:'paydate',sortable: true,width:200},
		{header:'联系电话',dataIndex:'telephone',sortable: true,width:200},
		{header:'欠费年度',dataIndex:'oweyear',sortable: true,width:200},
		{header:'缴费年度',dataIndex:'payyear',sortable: true,width:200},
		{header:'备注',dataIndex:'notes',sortable: true,width:200}
	];
	//新增信息
	var addInfo = function(){
		var me = this;
		var infowin = new PaymentsWindow('新增收费信息');
		infowin.show();
		infowin.on('submit',function(win,form,data){
			form.getForm().submit(
				{
					url:__ctxPath+'/back/fees/addpayments.html',
					method:'POST',
					success:function(form,action){
						if(action.result.success){
							var rec = new Ext.data.Record(data);
							rec.set('fid',action.result.id);
							rec.commit();
							store.insert(0,rec);
							Ext.MessageBox.alert('系统提示','添加成功!');
							win.close();
							me.getView().refresh();
						}else{
							Ext.MessageBox.alert('系统提示','添加失败!');
						}	
					},
					failure:function(form,action){
						Ext.MessageBox.alert('系统提示','网络原因 添加失败');
					}
				}
			);
		});
	};
	//修改信息
	var updateInfo = function(){
		var sel = this.getSelectionModel().getSelections();
		var win = new PaymentsWindow('修改收费信息');
		win.setIconClass('edit');
		if(sel.length!=1){
			Ext.MessageBox.alert('系统提示','只能选择一条数据对信息进行修改');
			return;
		}
		sel = this.getSelectionModel ().getSelected();
		win.show();
		win.load(sel);
		win.on('submit',function(win,form,data){
			form.getForm().submit(
				{
					url:__ctxPath+'/back/fees/updatepayments.html',
					method:'POST',
					success:function(form,action){
						if(action.result.success){
							var d = sel.data;
							for(var i in d){
								sel.set(i,data[i]);
							}
							sel.commit();
							win.close();
							Ext.MessageBox.alert('系统提示','修改成功!');
						
						}else{
							Ext.MessageBox.alert('系统提示','修改失败!');
						}	
					},
					failure:function(form,action){
						Ext.MessageBox.alert('系统提示','网络原因 修改失败');
					}
				}
			);
		})
	};
	//删除信息
	var deleteInfo = function(){
		var me = this;
		var sel = this.getSelectionModel().getSelections();
		//得到record的数组信息
		var ids = [];
		for (var i = 0; i < sel.length; i++) {
			ids.push(sel[i].get('fid'));
		}
		Ext.Msg.confirm('系统提示','是否确定删除选择的数据',function(btn){
			if(btn=='yes'){
				Ext.Ajax.request(
					{
						url:__ctxPath+'/back/fees/deletepayments.html',
						method:'POST',
						params:{ids:ids.join(',')},
						success:function(resp,option){
							var result = Ext.decode(resp.responseText);
							if(result.success){
								//服务器端成功删除后 在Store里面remove里面的数据
								for (var i = 0; i < sel.length; i++) {
									store.remove(sel[i]);
								}
								me.getView().refresh();
								Ext.MessageBox.alert('系统提示','删除成功');	
							}else{
								Ext.MessageBox.alert('系统提示','删除失败');
							}
						},
						failure:function(resp,option){
							Ext.MessageBox.alert('系统提示','网络原因,删除失败');
						}
					}
				);
			}
		});
	}
	
	
	var tbar = [
		{
			text:"新增信息",
			iconCls:"add",
			scope:this,
			handler:addInfo
		},'-',{
			text:'修改信息',
			iconCls:'edit',
			handler:updateInfo,
			scope:this
		},'-',
		{
			text:'删除信息',
			iconCls:'delete',
			scope:this,
			handler:deleteInfo
		},'-',
		{
			text:'查询信息',
			//iconCls:'zoom',
			scope:this,
			handler:function(){
					var win = new InfoWindow('信息查询');
					win.show();
					win.setIconClass('zoom');
					win.on('submit',function(win,form,data){
						var _data = data;
						for(var i in _data){
							if(_data[i]==null||_data[i]==''){
								delete _data[i];
							}
						}
						var _obj = {start:0,limit:limit};
						Ext.apply(_obj,_data);
						store.baseParams = _obj;
						store.reload();
						win.close();
					});
			}
		}
	];
	var bbar = [
		new Ext.PagingToolbar(
			{
				 store: store,       // grid and PagingToolbar using same store
        		 displayInfo: true,
        		 pageSize: limit
			}
		)
	];
	var config = {
		bbar:bbar,
		tbar:tbar,
		sm:sc,
		columns:cm,
		store:store,
		width:400
	};
	PaymentsGridPanel.superclass.constructor.call(this,config);
};
Ext.extend(PaymentsGridPanel,Ext.grid.GridPanel);

InfoPanel = function(limit){
	var grid = new PaymentsGridPanel(20);
	//alert('1111');
	var config = {
		items:[grid],
		layout:'fit',
		width:400,
		height:500,
		title:'取暖收费',
		iconCls:'zoom',
		id:'InfoPanel'
	};
	InfoPanel.superclass.constructor.call(this,config);
}
Ext.extend(InfoPanel,Ext.Panel);